import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const defaultSettings = {
  joinDate: new Date().toISOString().split('T')[0],
  salary: {
    type: 'monthly',
    amount: 18888
  },
  morningStart: '09:00',
  morningEnd: '12:00',
  afternoonStart: '13:30',
  afternoonEnd: '18:00',
  payday: 15,
  paydayTime: '12:00',
  workdays: [1, 2, 3, 4, 5]
}

export default new Vuex.Store({
  state: {
    workSettings: {
      ...JSON.parse(JSON.stringify(defaultSettings))
    }
  },
  mutations: {
    setWorkSettings(state, settings) {
      state.workSettings = settings
      wx.setStorageSync('workSettings', settings)
    },
    updateWorkSettings(state, settings) {
      state.workSettings = {
        ...state.workSettings,
        ...settings,
        baseSalary: Number(settings.baseSalary)
      }
    }
  },
  actions: {
    loadSettings({ commit }) {
      try {
        const settings = wx.getStorageSync('workSettings')
        if (settings) {
          commit('setWorkSettings', settings)
        } else {
          commit('setWorkSettings', JSON.parse(JSON.stringify(defaultSettings)))
        }
      } catch (error) {
        console.error('Load settings error:', error)
        commit('setWorkSettings', JSON.parse(JSON.stringify(defaultSettings)))
      }
    },
    saveSettings({ commit }, settings) {
      commit('setWorkSettings', settings)
    }
  }
}) 